package com.market.member.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.channel.service.IChannelCountInfoService;
import com.block.common.bean.CommonParam;
import com.block.common.bean.SessionBean;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.model.dto.LoginReqDTO;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.MemberBaseInfo;
import com.block.db.mapper.MemberBaseInfoMapper;
import com.market.member.enums.UserTypeEnum;
import com.market.member.factory.LoginTypeFactory;
import com.market.member.model.dto.LoginResultDTO;
import com.market.member.model.vo.UserInfoVO;
import com.market.member.service.IMemberBaseInfoService;
import com.market.member.service.IMemberLoginSessionService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-05
 */
@Service
public class MemberBaseInfoServiceImpl extends ServiceImpl<MemberBaseInfoMapper, MemberBaseInfo> implements IMemberBaseInfoService {

    private static final Logger log = LogFactory.getCommLog();

    @Autowired
    IMemberLoginSessionService memberLoginSessionService;
    @Autowired
    IChannelCountInfoService channelCountInfoService;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public ResponseVo toLogin(LoginReqDTO loginReqDTO, CommonParam commonParam){

        ResponseVo responseVo = LoginTypeFactory.getService(loginReqDTO.getLoginType()).toLogin(loginReqDTO);
        if (!AppRspCodeConstant.SUCCESS.equals(responseVo.getRspCode())){
            log.info("【用户登录】登录失败【result】{}", JSON.toJSONString(responseVo));
            return responseVo;
        }

        LoginResultDTO loginResultDTO = responseVo.getData().toJavaObject(LoginResultDTO.class);
        loginResultDTO.setLoginType(loginReqDTO.getLoginType());

        /**
         * 记录登陆token信息
         */
        SessionBean sessionBean = memberLoginSessionService.loginSession(loginResultDTO, commonParam);
        if (sessionBean == null){
            return new ResponseVo(AppRspCodeConstant.LOGIN_ERROR, "登录失败，请重试");
        }

        MemberBaseInfo memberBaseInfo = loginResultDTO.getMemberBaseInfo();
        /**
         * 获取用户 授权金额等信息
         */

        /**
         * 登陆注册完成之后，进行的操作
         * todo
         */
        // 随机用户不进行后续操作
        if (!ObjectUtil.equals(memberBaseInfo.getType(), UserTypeEnum.RANDOM.getValue())){
            this.afterLoginHandle(loginResultDTO, commonParam);
        }
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setToken(sessionBean.getToken());
        userInfoVO.setUid(String.valueOf(sessionBean.getUid()));
        userInfoVO.setUsername(sessionBean.getUsername());
        userInfoVO.setLimitAmount(memberBaseInfo.getCreditLimitAmount());

        userInfoVO.setChannelCode(sessionBean.getChannelCode());
        userInfoVO.setChannelName(sessionBean.getChannelName());
        userInfoVO.setRegChannelCode(sessionBean.getRegChannelCode());
        userInfoVO.setRegChannelName(sessionBean.getRegChannelName());

        userInfoVO.setAuthFlag(memberBaseInfo.getAuthFlag());
        userInfoVO.setFaceFlag(memberBaseInfo.getFaceFlag());
        userInfoVO.setSourceFlag(memberBaseInfo.getSourceFlag());

        return new ResponseVo(AppRspCodeConstant.SUCCESS, userInfoVO);
    }

    private void afterLoginHandle(LoginResultDTO loginResultDTO, CommonParam commonParam){

        channelCountInfoService.countLoginRegDataInfo(commonParam.getChannelCode(), loginResultDTO.isNewMember(), commonParam);

    }

    /**
     * 登陆之后获取用户信息
     * @param sessionBean
     * @param commonParam
     * @return
     */
    @Override
    public ResponseVo getUserInfo(SessionBean sessionBean, CommonParam commonParam) {

        MemberBaseInfo memberBaseInfo = this.getById(sessionBean.getUid());
        /**
         * 用户基本信息
         */
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setToken(sessionBean.getToken());
        userInfoVO.setUid(String.valueOf(sessionBean.getUid()));
        userInfoVO.setUsername(memberBaseInfo.getUsername());
        userInfoVO.setMobile(DesensitizedUtil.mobilePhone(memberBaseInfo.getMobile()));
        userInfoVO.setLimitAmount(memberBaseInfo.getCreditLimitAmount());

        userInfoVO.setChannelCode(sessionBean.getChannelCode());
        userInfoVO.setChannelName(sessionBean.getChannelName());
        userInfoVO.setRegChannelCode(sessionBean.getRegChannelCode());
        userInfoVO.setRegChannelName(sessionBean.getRegChannelName());

        userInfoVO.setAuthFlag(memberBaseInfo.getAuthFlag());
        userInfoVO.setFaceFlag(memberBaseInfo.getFaceFlag());
        userInfoVO.setSourceFlag(memberBaseInfo.getSourceFlag());

        return new ResponseVo(AppRspCodeConstant.SUCCESS, userInfoVO);
    }

}
